/**
 * 
 */
package com.et114.modules.org.dao;

import com.et114.components.acegi.Constants;
import com.et114.core.common.AppContext;
import com.et114.core.dao.impl.BaseDAOImpl;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.vo.AclVo;

import org.apache.commons.beanutils.PropertyUtils;
/*
 * @author guanhw
 * deleteAclByRoleId
 * deleteAclByUserId
 * deleteAclByTitleId
 * deleteAclByFuncId
 * deleteAclByMethodId
 * deleteAclByFuncElementId
 */
public class AclDAO extends BaseDAOImpl {
	public void deleteAcl ( AclVo acl  )  throws CheckException { 
		if ( AppContext.isHasLog ( )  ) {
			AclVo dvo = new AclVo() ; 
			try {
				PropertyUtils.copyProperties ( dvo , acl ) ;
			}
			catch ( Exception e ) {
				logger.info ( e.getMessage ( ) , e ) ;
			}
			dvo = loadAcl( dvo ) ; 
			if ( dvo != null ) {
				dvo.setOperType ( Constants.LOG_OPER_REMOVE ) ;
				dvo.setOperUser ( ( acl.getOperUser ( ) != null && acl.getOperUser ( ).longValue ( ) != 0 ) ? acl.getOperUser ( ) : acl.getCrtUser  ( ) ) ; 
				saveLogAcl( dvo ) ;								
			}
		}	    
		delete ( acl , "deleteAcl" ) ; 		
	}
	
	public void deleteAclByRoleId( Long roleId , Long operUser )  throws CheckException { 
		//delete ( roleId , "deleteAclByRoleId" ) ; 
		AclVo acl = new AclVo(); 
		acl.setOperUser ( operUser ) ; 
		acl.setPrincipalId ( roleId ) ; 
		acl.setPrincipalType ( Constants.PRINCIPALID_ROLE ) ; 
		deleteAcl( acl  ) ; 
	}
	
	public void deleteAclByUserId ( Long userId , Long operUser ) throws CheckException { 
		//delete( userId , "deleteAclByUserId" ) ;
		AclVo acl = new AclVo(); 
		acl.setOperUser ( operUser ) ; 
		acl.setPrincipalId ( userId ) ; 
		acl.setPrincipalType ( Constants.PRINCIPALID_USER ) ; 
		deleteAcl( acl  ) ; 		
	}
	
	public void deleteAclByTitleId ( Long titleId , Long operUser ) throws CheckException { 
		//delete( titleId , "deleteAclByTitleId" ) ;
		AclVo acl = new AclVo(); 
		acl.setOperUser ( operUser ) ; 
		acl.setPrincipalId ( titleId ) ; 
		acl.setPrincipalType ( Constants.PRINCIPALID_TITLE ) ; 
		deleteAcl( acl  ) ; 				
	}
	
	public void deleteAclByFuncId ( Long id , Long operUser ) throws CheckException { 
		//delete( id , "deleteAclByFuncId" ) ;
		AclVo acl = new AclVo(); 
		acl.setOperUser ( operUser ) ; 
		acl.setResourceId ( id ) ;
		acl.setResourceType ( Constants.RESOURCE_URL ) ; 
		deleteAcl( acl  ) ; 						
	}
	
	public void deleteAclByMethodId ( Long id , Long operUser  ) throws CheckException { 
		//delete( id , "deleteAclByMethodId" ) ;
		AclVo acl = new AclVo(); 
		acl.setOperUser ( operUser ) ; 
		acl.setResourceId ( id ) ;
		acl.setResourceType ( Constants.RESOURCE_METHOD ) ; 
		deleteAcl( acl  ) ; 										
	}

	public void deleteAclByFuncElementId ( Long id , Long operUser ) throws CheckException { 
		//delete( id , "deleteAclByFuncElementId" ) ;
		AclVo acl = new AclVo(); 
		acl.setOperUser ( operUser ) ; 
		acl.setResourceId ( id ) ;
		acl.setResourceType ( Constants.RESOURCE_COMPONENT ) ; 
		deleteAcl( acl  ) ; 								
	}
	
	public void saveLogAcl( AclVo acl ) throws CheckException {
		insert( acl ,  "insertLogAcl" ) ; 
	}
	
	/**
	 * 0 存在： 1 授权成功
	 * @param acl
	 * @return
	 * @throws CheckException
	 */
	public void  doAuthorize ( AclVo acl )  throws CheckException { 
	    //if ( countloadAuthorizes( acl ) == 0 ) {
			insert( acl , "doAuthorize" ) ;
			if ( AppContext.isHasLog ( ) ) {
				acl.setOperUser (  acl.getCrtUser ( ) ) ;
				acl.setOperType ( Constants.LOG_OPER_NEW ) ;
				saveLogAcl( acl ) ;
			}	    	
	    //}
	}
	
	public int countloadAuthorizes( AclVo acl ) throws CheckException {
		return getCount ( acl , "countloadAuthorizes" ).intValue ( ) ;
	}
	
	
	
	public AclVo loadAcl(  AclVo acl ) throws CheckException {
		return ( AclVo )load ( acl , "loadAuthorizes" ) ;
	}	
}
